﻿using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
using SXFramWork.Core;

namespace SXFramWork.Core.Quartz
{
    /// <summary>
    /// Quartz配置
    /// </summary>
    public class QuartzConfig 
    {
        /// <summary>
        /// 开启
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 启用日志
        /// </summary>
        public bool Logger { get; set; } = true;

        /// <summary>
        /// 实例名称
        /// </summary>
        [Required(ErrorMessage = "实例名称不能为空")]
        public string InstanceName { get; set; } = "QuartzServer";

        /// <summary>
        /// 表前缀
        /// </summary>
        public string TablePrefix { get; set; } = "QRTZ_";

        /// <summary>
        /// 序列化方式
        /// </summary>
        public QuartzSerializerType SerializerType { get; set; } = QuartzSerializerType.Json;

        /// <summary>
        /// 数据库类型
        /// </summary>
        public QuartzProvider Provider { get; set; } = QuartzProvider.SqlServer;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        [Required(ErrorMessage = "数据库连接字符串不能为空")]
        public string ConnectionString { get; set; } = ConfigHelper.app(new string[] { "DBConnection", "SqlServer" });

        /// <summary>
        /// 数据源
        /// </summary>
        public string DataSource { get; set; } = "SX";

        public NameValueCollection ToProps()
        {
            var dbtype= ConfigHelper.app(new string[] { "DBConnection", "Type" }).To_Int();
            if (dbtype == 1)
            {
                this.ConnectionString= ConfigHelper.app(new string[] { "DBConnection", "MySql" });
            }
            return new NameValueCollection
            {
                ["quartz.scheduler.instanceName"] = InstanceName,
                ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX,Quartz",
                ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate,Quartz",
                ["quartz.jobStore.tablePrefix"] = TablePrefix,
                ["quartz.jobStore.dataSource"] = DataSource,
                ["quartz.dataSource.default.connectionString"] = ConnectionString,
                ["quartz.dataSource.default.provider"] = Provider.ToDescription(),
                ["quartz.serializer.type"] = SerializerType.ToDescription()
            };
        }
    }
}
